package com.ruoyi.system.service.impl;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.domain.entity.FoodStock;
import com.ruoyi.common.core.domain.entity.RestaurantStoreResource;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.mapper.FoodStockMapper;
import com.ruoyi.system.mapper.RestaurantStoreResourceMapper;
import com.ruoyi.system.service.IFoodStockService;
import com.ruoyi.system.service.RestaurantStoreResourceService;
import com.ruoyi.system.service.req.CreateResourceDTO;
import com.ruoyi.system.service.req.UpdateResourceDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: ruohao
 * @Date: 2023/8/6
 **/
@Service
public class RestaurantStoreResourceServiceImpl implements RestaurantStoreResourceService {
    private static final Logger log = LoggerFactory.getLogger(RestaurantStoreResourceServiceImpl.class);

    @Resource
    private RestaurantStoreResourceMapper restaurantStoreResourceMapper;

    @Resource
    IFoodStockService foodStockService;

    @Resource
    private FoodStockMapper foodStockMapper;

    @Override
    public List<RestaurantStoreResource> selectRestaurantStoreResourceList(RestaurantStoreResource resource) {
        return restaurantStoreResourceMapper.selectRestaurantStoreResourceList(resource);
    }

    @Override
    public RestaurantStoreResource selectRestaurantStoreResourceById(Long id) {
        return restaurantStoreResourceMapper.selectRestaurantStoreResourceById(id);
    }

    @Override
    public List<RestaurantStoreResource> selectRestaurantStoreResourceByIds(List<Long> ids) {
        return restaurantStoreResourceMapper.selectRestaurantStoreResourceByIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean createRestaurantStoreResource(CreateResourceDTO createResourceDTO) {
        RestaurantStoreResource createResourceDO = CreateResourceDTO.convertToDO(createResourceDTO);
        createResourceDO.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());

        if (StringUtils.isNotBlank(createResourceDO.getName())) {
            RestaurantStoreResource query = new RestaurantStoreResource();
            query.setName(createResourceDO.getName());
            List<RestaurantStoreResource> resourceList = restaurantStoreResourceMapper.selectRestaurantStoreResourceListQuery(query);
            if (!CollectionUtils.isEmpty(resourceList)) {
                throw new BaseException("已存在该食材，请勿重复添加" + createResourceDO.getName());
            }
        }

        int result = restaurantStoreResourceMapper.insertRestaurantStoreResource(createResourceDO);
        if (result == 1) {
            //往库存表里插入2条数据
            List<String> areas = Arrays.asList("二区", "一区");
            for (String area : areas) {
                FoodStock foodStock = new FoodStock();
                BeanUtils.copyProperties(createResourceDO, foodStock);
                foodStock.setArea(area);
                foodStock.setId(null);
                foodStock.setAmount(BigDecimal.ZERO);
                foodStock.setLastAmount(BigDecimal.ZERO);
                foodStock.setResourceId(createResourceDO.getId());
                foodStock.setAvgPrice(createResourceDO.getAvgPrice() == null ? BigDecimal.ZERO : createResourceDO.getAvgPrice());
                foodStock.setUp(createResourceDO.getUp() == null ? new BigDecimal(100) : createResourceDO.getUp());
                foodStock.setDown(createResourceDO.getDown() == null ? BigDecimal.ZERO : createResourceDO.getDown());
                foodStockService.insertFoodStock(foodStock);
            }
            return Boolean.TRUE;
        }
        log.warn("插入食材到数据库失败 resourceDO {} result {}", JSON.toJSONString(createResourceDO), result);
        return Boolean.FALSE;
    }

    @Override
    public Boolean updateRestaurantStoreResource(UpdateResourceDTO updateResourceDTO) {
        RestaurantStoreResource updateDO = UpdateResourceDTO.convertToDO(updateResourceDTO);
        updateDO.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        int result = restaurantStoreResourceMapper.updateRestaurantStoreResource(updateDO);
        if (result == 1) {
            FoodStock query = new FoodStock();
            query.setResourceId(updateDO.getId());
            List<FoodStock> foodStockList = foodStockService.selectRestaurantStoreResourceList(query);
            for (FoodStock foodStock : foodStockList) {
                Long id = foodStock.getId();
                String area = foodStock.getArea();
                BigDecimal amount = foodStock.getAmount();
                BigDecimal lastAmount = foodStock.getLastAmount();
                BeanUtils.copyProperties(updateDO, foodStock);
                foodStock.setArea(area);
                foodStock.setId(id);
                foodStock.setAmount(amount);
                foodStock.setLastAmount(lastAmount);
                foodStock.setResourceId(updateDO.getId());
                foodStock.setAvgPrice(updateDO.getAvgPrice() == null ? BigDecimal.ZERO : updateDO.getAvgPrice());
                foodStock.setUp(updateDO.getUp() == null ? new BigDecimal(1000) : updateDO.getUp());
                foodStock.setDown(updateDO.getDown() == null ? BigDecimal.ZERO : updateDO.getDown());
                foodStockMapper.updateRestaurantStoreResource(foodStock);
            }
            return Boolean.TRUE;
        }
        log.warn("更新食材到数据库失败 resourceDO {} result {}", JSON.toJSONString(updateDO), result);
        return Boolean.FALSE;
    }

    @Override
    public Boolean deleteRestaurantStoreResourceById(Long id) {
        int result = restaurantStoreResourceMapper.deleteRestaurantStoreResourceById(id);
        if (result == 1) {
            return Boolean.TRUE;
        }
        log.warn("删除食材到数据库失败 id {} result {}", id, result);
        return Boolean.FALSE;
    }

    @Override
    public int deleteRestaurantStoreResourceByIds(List<Long> ids) {
        log.info("deleteRestaurantStoreResourceByIds ids {}", JSON.toJSONString(ids));
        return restaurantStoreResourceMapper.deleteRestaurantStoreResourceByIds(ids);
    }
}
